package com.zmj.tl.database.taos.service.impl;

import com.zmj.tl.database.taos.domain.MeterMetric;
import com.zmj.tl.database.taos.domain.MeterSchema;
import com.zmj.tl.database.taos.mapper.MeterExecuteMapper;
import com.zmj.tl.database.taos.service.MeterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author hd
 * @desc
 * @since 2024/1/29 10:58
 */
@Slf4j
@Service("meterService")
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class MeterServiceImpl implements MeterService {
    private final MeterExecuteMapper executeMapper;

    /**
     * @desc 创建超级表
     * @author hd
     * @since 2024/2/1 13:24
     **/
    @Override
    public int createTable(MeterSchema meterSchema) {
        return this.executeMapper.createTable(meterSchema.getTbName(), meterSchema.getMetricSchema(), meterSchema.getTagSchema());
    }

    /**
     * @desc 创建子表
     * @author hd
     * @since 2024/2/1 13:24
     **/
    @Override
    public int createStable(MeterSchema meterSchema) {
        return this.executeMapper.createStable(meterSchema.getStbName(), meterSchema.getTbName(), meterSchema.getTagColumn());
    }

    /**
     * @desc 删除超级表
     * @author hd
     * @since 2024/2/1 13:24
     **/
    @Override
    public void dropTable(MeterSchema meterSchema) {
        this.executeMapper.dropTable(meterSchema.getTbName());
    }

    /**
     * @param meterSchema 约束
     * @desc 删除库
     * @author hd
     * @since 2024/2/1 13:24
     **/
    @Override
    public void dropDataBase(MeterSchema meterSchema) {
        this.executeMapper.dropDatabase(meterSchema.getDbName());
    }

    /**
     * @param metric 表计参数
     * @desc 新增表计参数
     * @author hd
     * @since 2024/2/1 12:34
     **/
    @Override
    public void addOne(MeterMetric metric) {
        MeterSchema meterSchema = new MeterSchema();
        meterSchema.setStbName(metric.getStbName());
        executeMapper.insertOne(meterSchema, metric);
    }

    /**
     * @param metric 表计参数
     * @desc 新增表计参数
     * @author hd
     * @since 2024/2/1 12:35
     **/
    @Override
    public void addEntity(MeterMetric metric) {
        MeterSchema meterSchema = new MeterSchema();
        meterSchema.setStbName(metric.getStbName());
        executeMapper.insertEntity(meterSchema, metric);
    }

    /**
     * @param metric 表计参数
     * @desc 批量新增表计参数
     * @author hd
     * @since 2024/2/1 12:35
     **/
    @Override
    public void addBatch(MeterMetric metric) {
        MeterSchema meterSchema = new MeterSchema();
        executeMapper.insertBatch(meterSchema, metric, metric.getList());
    }
}
